import { uuid } from 'g6-fn'
import { defineStore } from 'pinia'
import { ref } from 'vue'

import { MarkTaskLabelRes } from '../../components/LabelCard/types'
import { EntityLabelLocationInfo } from '../utils/dmp-text/config'

export interface RelationResultType {
  id: string
  uid: string
  relationLabel: MarkTaskLabelRes
  startLabel: EntityLabelLocationInfo
  endLabel: EntityLabelLocationInfo
}

export const useLabelResultListStore = defineStore(
  'labelResultListStore',
  () => {
    // 关系标签关联结果
    const relationListLoading = ref(false)
    const relationResultList = ref<RelationResultType[]>([])

    // 新增标注结果
    const addRelationResult = (startLabel: EntityLabelLocationInfo, endLabel: EntityLabelLocationInfo, relationLabel: MarkTaskLabelRes) => {
      return new Promise((resolve, reject) => {
        // 判断是否有重复的标注结果
        const hasRepeat = relationResultList.value.some(item => {
          return item.startLabel.onlyId === startLabel.onlyId && item.endLabel.onlyId === endLabel.onlyId
        })

        if (hasRepeat) {
          reject('此结果已存在于标注结果中')
        } else {
          relationResultList.value.unshift({
            id: '',
            uid: uuid(),
            startLabel,
            endLabel,
            relationLabel
          })
          resolve('新增成功')
        }
      })
    }

    // 修改关系标签
    const updateRelationResult = (item: RelationResultType, relationLabel: MarkTaskLabelRes) => {
      relationResultList.value = relationResultList.value.map(i => {
        if (i.uid === item.uid) {
          return {
            ...i,
            relationLabel
          }
        }
        return i
      })
    }

    /**
     * 根据标签ID修改实体标签信息
     * @param labelId 实体标签ID
     * @param labelInfo 需要修改的内容
     */
    const updateEntityLabelByLabelId = (labelId: string, labelInfo: EntityLabelLocationInfo) => {
      relationResultList.value = relationResultList.value.map(item => {
        if (item.startLabel.labelId === labelId) {
          item.startLabel = {
            ...item.startLabel,
            ...labelInfo
          }
        }
        if (item.endLabel.labelId === labelId) {
          item.endLabel = {
            ...item.endLabel,
            ...labelInfo
          }
        }

        return item
      })
    }

    // 删除结果
    const removeRelationResult = (item: RelationResultType) => {
      relationResultList.value = relationResultList.value.filter(i => i.uid !== item.uid)
    }

    // 批量删除结果
    const removeRelationResults = (items: RelationResultType[]) => {
      const idList = items.map(i => i.uid)
      relationResultList.value = relationResultList.value.filter(i => !idList.includes(i.uid))
    }

    /**
     * 删除跟实体标签关联的标注结果
     * @param labelId 实体标签ID
     */
    const removeRelationResultsByEntityLabel = (labelId: string) => {
      relationResultList.value = relationResultList.value.filter(i => i.startLabel.labelId !== labelId && i.endLabel.labelId !== labelId)
    }

    // 远程数据转换为本地数据
    const remoteToLocalResult = (remoteData: any[]) => {
      if (!remoteData.length) {
        return
      }
      const localData = remoteData.map((item: any) => {
        // 关系标签
        const relationLabel: MarkTaskLabelRes = {
          id: item.taskLabelId,
          name: item.labelName,
          beginLabelId: item.beginLabelResultId,
          beginLabelName: item.beginLabelName,
          endLabelId: item.endLabelResultId,
          endLabelName: item.endLabelName
        }

        // 起始标签
        const startLabel: EntityLabelLocationInfo = {
          onlyId: item.beginLabelResultId,
          labelId: item.beginLabelId,
          labelName: item.beginLabelName,
          labelColor: item.beginLabelColor,
          text: item.beginLabelValue,
          start: item.beginLabelStartPos,
          end: item.beginLabelEndPos
        }

        // 结束标签
        const endLabel: EntityLabelLocationInfo = {
          onlyId: item.endLabelResultId,
          labelId: item.endLabelId,
          labelName: item.endLabelName,
          labelColor: item.endLabelColor,
          text: item.endLabelValue,
          start: item.endLabelStartPos,
          end: item.endLabelEndPos
        }

        return {
          id: item.id,
          uid: uuid(),
          relationLabel,
          startLabel,
          endLabel
        }
      })

      relationResultList.value = localData || []
    }

    // 重置标注结果
    const reset = () => {
      relationResultList.value = []
      relationListLoading.value = false
    }

    return {
      relationResultList,
      relationListLoading,
      addRelationResult,
      reset,
      remoteToLocalResult,
      updateRelationResult,
      removeRelationResult,
      removeRelationResults,
      updateEntityLabelByLabelId,
      removeRelationResultsByEntityLabel
    }
  },
  {
    persist: false //本地持久化存储
  }
)
